package org.telegram.bot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.ruoyi.common.exception.ServiceException;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.telegram.bot.domain.BotBanEmoji;
import org.telegram.bot.mapper.BotBanEmojiMapper;
import org.telegram.bot.mq.producer.GroupCacheRefreshProducer;
import org.telegram.bot.service.IBotBanEmojiService;

import java.util.Arrays;
import java.util.List;

/**
 * 违禁情Service业务层处理
 *
 * @author wyx
 * @date 2024-11-30
 */
@Slf4j
@Service
public class BotBanEmojiServiceImpl implements IBotBanEmojiService
{
    @Resource
    private BotBanEmojiMapper botBanEmojiMapper;
    
    @Resource
    private GroupCacheRefreshProducer groupCacheRefreshProducer;
    /**
     * 查询违禁情
     *
     * @param id 违禁情主键
     * @return 违禁情
     */
    @Override
    public BotBanEmoji selectBotBanEmojiById(Long id)
    {
        return botBanEmojiMapper.selectById(id);
    }

    /**
     * 查询违禁情列表
     *
     * @param botBanEmoji 违禁情
     * @return 违禁情
     */
    @Override
    public List<BotBanEmoji> selectBotBanEmojiList(BotBanEmoji botBanEmoji)
    {
        return botBanEmojiMapper.selectBotBanEmojiList(botBanEmoji);
    }

    /**
     * 新增违禁情
     *
     * @param botBanEmoji 违禁情
     * @return 结果
     */
    @Override
    public int insertBotBanEmoji(BotBanEmoji botBanEmoji)
    {
        Long l = botBanEmojiMapper.selectCount(new LambdaQueryWrapper<BotBanEmoji>().eq(BotBanEmoji::getEmoji, botBanEmoji.getEmoji()));
        if(SqlHelper.retBool(l)){
            throw new ServiceException("表情已存在");
        }
        botBanEmojiMapper.insert(botBanEmoji);
        groupCacheRefreshProducer.sendMessage();
        return 1;
    }

    /**
     * 修改违禁情
     *
     * @param botBanEmoji 违禁情
     * @return 结果
     */
    @Override
    public int updateBotBanEmoji(BotBanEmoji botBanEmoji)
    {
        Long l = botBanEmojiMapper.selectCount(new LambdaQueryWrapper<BotBanEmoji>().ne(BotBanEmoji::getId, botBanEmoji.getId()).eq(BotBanEmoji::getEmoji, botBanEmoji.getEmoji()));
        if(SqlHelper.retBool(l)){
            throw new ServiceException("表情已存在");
        }
        botBanEmojiMapper.updateById(botBanEmoji);
        groupCacheRefreshProducer.sendMessage();
        return 1;
    }

    /**
     * 批量删除违禁情
     *
     * @param ids 需要删除的违禁情主键
     * @return 结果
     */
    @Override
    public int deleteBotBanEmojiByIds(Long[] ids)
    {
        botBanEmojiMapper.deleteByIds(Arrays.asList(ids));
        groupCacheRefreshProducer.sendMessage();
        return 1;
    }

    /**
     * 删除违禁情信息
     *
     * @param id 违禁情主键
     * @return 结果
     */
    @Override
    public int deleteBotBanEmojiById(Long id)
    {
        botBanEmojiMapper.deleteById(id);
        groupCacheRefreshProducer.sendMessage();
        return 1;
    }
    
    
    @Override
    public int deleteBotBanEmojiByChatId(Long chatId) {
       return botBanEmojiMapper.delete(new LambdaQueryWrapper<BotBanEmoji>().eq(BotBanEmoji::getChatId,chatId));
    }
}
